package com.ivideo.backend.base.exception;

import com.ivideo.backend.base.utils.Result;
import com.ivideo.backend.base.utils.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Project: ivideo-parent
 * @Name: GlobalExceptionHandler
 * @Author: Jack_Li
 * @Date: 2024/5/20 13:43
 */

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    //特定异常
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody //为了返回数据
    public Result<String> error(ArithmeticException e) {
        e.printStackTrace();
        return Result.error(ResultCodeEnum.RC500.getCode(),"执行了ArithmeticException异常处理..");
    }

    //自定义异常
    @ExceptionHandler(CustomException.class)
    @ResponseBody //为了返回数据
    public Result<String> error(CustomException e) {
        return Result.error(ResultCodeEnum.get(e.getCode()).getCode(), ResultCodeEnum.get(e.getCode()).getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result<String> handleException(Exception e) {
        log.error("全局异常信息: {}", e.getMessage(), e);
        return Result.error(ResultCodeEnum.RC999.getCode(), e.getMessage());
    }
}
